﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VersionComparer.Base;
using VersionComparer.UI;

namespace VersionComparer.Views
{
    public partial class RichTextComparers : UserControl, IContainedControl
    {
        Timer initTimer = new Timer();
        public RichTextComparers()
        {
            InitializeComponent();
            Text = Resources.Resources.DetailsCaption;
        }

        private void RichTextComparers_Load(object sender, EventArgs e)
        {
            initTimer.Tick += initTimer_Tick;
            initTimer.Interval = 10;
            initTimer.Start();
            
        }

        void initTimer_Tick(object sender, EventArgs e)
        {
            initTimer.Stop ();
            DoubleBuffered = true;
            RichTextComparer comparer1 = new RichTextComparer (true);
            tabControl1.Size = Size;
            comparer1.Visible = false;
            RichTextComparer comparer2 = new RichTextComparer (false);
            comparer1.Rtf1 = Difference1;
            comparer1.Rtf2 = Difference2;
            comparer1.Dock = DockStyle.Fill;
            comparer2.Dock = DockStyle.Fill;
            comparer2.Rtf1 = Original1;
            comparer2.Rtf2 = Original2;

            comparer1.Size = tabControl1.TabPages[0].Size;
            tabControl1.TabPages[0].Controls.Add(comparer1);
            
            if (Original1.Rtf == Difference1.Rtf && Original2.Rtf == Difference2.Rtf)
            {
                tabControl1.TabPages.RemoveAt(1);
                tabControl1.TabPages[0].Text = "Original";
            }
            else
            tabControl1.TabPages[1].Controls.Add(comparer2);
           
            tabControl1.Visible = true;
            comparer1.Visible = true;
            cbLock.Checked = SelectedComparer.Locked;
        }

        public RtfBuilder Difference1
        {
            get;
            set;
        }
        public RtfBuilder Difference2
        {
            get;
            set;
        }
        public RtfBuilder Original1
        {
            get;
            set;
        }
        public RtfBuilder Original2
        {
            get;
            set;
        }

        private void cbLock_CheckedChanged(object sender, EventArgs e)
        {
            SelectedComparer.Locked = cbLock.Checked;
        }

        private RichTextComparer SelectedComparer
        {
            get
            {
                return tabControl1.SelectedTab.Controls[0] as RichTextComparer;
            }
        }
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbLock.Checked = SelectedComparer.Locked;
        }

        string IContainedControl.Text
        {
            get
            {
                return Resources.Resources.RichTextComparerCaption;
            }
        }


        public void Close(bool acceptChanges)
        {
        }
    }
}
